package com.hf.eagle.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hf.eagle.dao.AccessoryDao;
import com.hf.eagle.dao.AccessoryRelDao;
import com.hf.eagle.dao.NewsInfoDao;
import com.hf.eagle.dao.PromotionDescDao;
import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.AccessoryRel;
import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.domain.PromotionDesc;
import com.hf.eagle.service.NewsInfoService;

/**
 * @author Xu,XiaoYuan
 * @version 2011-09-06 23:44
 */
@Service
public class NewsInfoServiceImpl implements NewsInfoService {

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private AccessoryDao accessoryDao;

	@Resource
	private AccessoryRelDao accessoryRelDao;

	@Resource
	private PromotionDescDao promotionDescDao;

	public Long createNewsInfo(NewsInfo t) {
		Long mod_id = t.getMod_id();

		Long news_id = this.newsInfoDao.insertEntity(t);

		List<Accessory> accList = t.getAccessoryList();
		if (null != accList && accList.size() > 0) {
			for (Accessory accessory : accList) {
				Long acce_id = accessoryDao.insertEntity(accessory);

				AccessoryRel accessoryRel = new AccessoryRel();
				accessoryRel.setMod_id(mod_id);
				accessoryRel.setRecord_id(news_id);
				accessoryRel.setAcce_id(acce_id);

				accessoryRelDao.insertEntity(accessoryRel);
			}
		}
		return news_id;
	}

	public NewsInfo getNewsInfo(NewsInfo t) {
		return this.newsInfoDao.selectEntity(t);
	}

	public Long getNewsInfoCount(NewsInfo t) {
		return this.newsInfoDao.selectEntityCount(t);
	}

	public List<NewsInfo> getNewsInfoList(NewsInfo t) {
		return this.newsInfoDao.selectEntityList(t);
	}

	public int modifyNewsInfo(NewsInfo t) {
		List<Accessory> accList = t.getAccessoryList();
		if (null != accList && accList.size() > 0) {
			/************ 删除原先的附件 ************/
			AccessoryRel acce_rel = new AccessoryRel();
			acce_rel.setMod_id(t.getMod_id());
			acce_rel.setRecord_id(t.getId());
			List<AccessoryRel> list = accessoryRelDao.selectEntityList(acce_rel);

			String[] pks = null;
			if (null != list && list.size() > 0) {
				pks = new String[list.size()];
				for (int i = 0; i < list.size(); i++) {
					pks[i] = String.valueOf(list.get(i).getAcce_id());

					accessoryRelDao.deleteEntity(list.get(i));
				}
			}

			if (null != pks && pks.length > 0) {
				Accessory acce = new Accessory();
				acce.getMap().put("pks", pks);
				accessoryDao.deleteEntity(acce);
			}
			/************ 删除原先的附件 ************/

			for (Accessory accessory : accList) {
				Long acce_id = accessoryDao.insertEntity(accessory);

				AccessoryRel accessoryRel = new AccessoryRel();
				accessoryRel.setMod_id(t.getMod_id());
				accessoryRel.setRecord_id(t.getId());
				accessoryRel.setAcce_id(acce_id);

				accessoryRelDao.insertEntity(accessoryRel);
			}
		} else if (null != t.getMap().get("acce_id") && !"".equals((String) t.getMap().get("acce_id"))) {// 修改原附件相关信息(标题等)
			String mod_id = (String) t.getMap().get("mod_id");
			String description = (String) t.getMap().get("description");

			Accessory acce = new Accessory();
			acce.setDescription(description);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", t.getId());

			accessoryDao.updateEntity(acce);
		}

		return this.newsInfoDao.updateEntity(t);
	}

	public int removeNewsInfo(NewsInfo t) {
		// /************ 删除附件 (逻辑删除) ************/
		// AccessoryRel acce_rel = new AccessoryRel();
		// acce_rel.setMod_id(t.getMod_id());
		// if (null != t.getId()) {
		// acce_rel.setRecord_id(t.getId());
		// } else if (null != t.getMap().get("pks")) {
		// String[] record_ids = (String[]) t.getMap().get("pks");
		// acce_rel.getMap().put("record_ids", StringUtils.join(record_ids, ","));
		// }
		//
		// String[] pks = null;
		// List<AccessoryRel> list = accessoryRelDao.selectEntityList(acce_rel);
		// if (null != list && list.size() > 0) {
		// pks = new String[list.size()];
		// for (int i = 0; i < list.size(); i++) {
		// pks[i] = String.valueOf(list.get(i).getAcce_id());
		// }
		// }
		//
		// if (null != pks && pks.length > 0) {
		// Accessory acce = new Accessory();
		// acce.setIs_del(1);
		// acce.getMap().put("pks", pks);
		// accessoryDao.updateEntity(acce);
		// }
		// /************ 删除附件(逻辑删除) ************/

		return this.newsInfoDao.updateEntity(t);
	}

	public List<NewsInfo> getNewsInfoPaginatedList(NewsInfo t) {
		return this.newsInfoDao.selectEntityPaginatedList(t);
	}

	public Long createPromotionInfo(NewsInfo t) {
		Long news_id = this.createNewsInfo(t);

		List<PromotionDesc> list = t.getPromotionDescList();
		if (null != list && list.size() > 0) {
			for (PromotionDesc promotionDesc : list) {
				promotionDesc.setNews_id(news_id);
				promotionDescDao.insertEntity(promotionDesc);
			}
		}

		return news_id;
	}

	public int modifyPromotionInfo(NewsInfo t) {
		int result = this.modifyNewsInfo(t);

		List<PromotionDesc> list = t.getPromotionDescList();
		if (null != list && list.size() > 0) {
			PromotionDesc pd = new PromotionDesc();
			pd.getMap().put("news_id", t.getId());
			promotionDescDao.deleteEntity(pd);

			for (PromotionDesc promotionDesc : list) {
				promotionDesc.setNews_id(t.getId());
				promotionDescDao.insertEntity(promotionDesc);
			}
		}

		return result;
	}

}
